﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using System.Text;

public partial class TotalPlan_TotalPlanDisplay : System.Web.UI.Page
{
    Authentication dllAuthentication = new Authentication();
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isLogin();
        if (!IsPostBack)
        {
            bindDropDownList(this.drpYear);

            if (Request.Params["TotalYear"] != null && Request.Params["TotalYear"].ToString().Length != 0)
            {
                string Year = Request.Params["TotalYear"].ToString();
                this.drpYear.SelectedValue = Year;
            }
            bindGridView();
        }
    }
    /// <summary>
    /// 绑定下拉列表
    /// </summary>
    /// <param name="_drp"></param>
    void bindDropDownList(DropDownList _drp)
    {
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(li);
            }
            //根据系统日期自动绑定到当前年份
            string _year = DateTime.Now.Year.ToString();
            this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));
        }
    }

    /// <summary>
    /// 绑定GridView
    /// </summary>
    void bindGridView()
    {
        TotalPlanMgr dllTotalPlanMgr = new TotalPlanMgr();
        object[] _value = new object[1];
        _value[0] = this.drpYear.SelectedItem.Text;
        DataSet ds = dllTotalPlanMgr.getTotalPlan(_value);
        if (ds.Tables.Count > 1)
        {
            DataTable dt = ds.Tables[ds.Tables.Count - 1];
            DataView dv = new DataView(dt);
            dv.Sort = "CompleteTime";
            this.gdvTotalPlan.DataSource = dv;
            this.gdvTotalPlan.DataBind();
        }
        else
        {
            this.gdvTotalPlan.DataSource = new DataTable();
            this.gdvTotalPlan.DataBind();
        }
        FormatGridView();
        MergeRow(this.gdvTotalPlan);
    }


    /// <summary>
    /// 处理GridView链接 颜色
    /// </summary>
    void FormatGridView()
    {
        DataTable dt = new DataTable();
        DataTable quarter = new DataTable();
        object[] _value = new object[1];
        object[] _valueQuarter = new object[1];
        TotalPlanMgr dllTotalPlanMgr = new TotalPlanMgr();
        QuarterPlanofManager dllQuarterPlanofManager = new QuarterPlanofManager();
        for (int i = 0; i < this.gdvTotalPlan.Rows.Count; i++)
        {
            for (int j = 2; j < this.gdvTotalPlan.Rows[i].Cells.Count; j++)
            {
                this.gdvTotalPlan.Rows[i].Cells[j].Text = this.gdvTotalPlan.Rows[i].Cells[j].Text.Trim();
                if (this.gdvTotalPlan.Rows[i].Cells[j].Text != "&nbsp;" && this.gdvTotalPlan.Rows[i].Cells[j].Text.Length > 0)
                {
                    this.gdvTotalPlan.Rows[i].Cells[j].Text = AddHyperLink(this.gdvTotalPlan.Rows[i].Cells[j].Text);
                }
            }
        }
    }

    /// <summary>
    /// 计划 加链接
    /// </summary>
    /// <param name="_planContent"></param>
    string AddHyperLink(string _planContent)
    {
        StringBuilder sb = new StringBuilder();
        TotalPlanMgr dllTotalPlanMgr = new TotalPlanMgr();
        _planContent = _planContent.Remove(_planContent.Length - 1);
        string[] plan = _planContent.Split('@');
        string link = "<a target='_blank' href='QuarterPlanDetail.aspx?QuarterPlanId=";
        string _segments = "TotalPlanDisplay.aspx";
        string url = "&Url=" + _segments + "&TotalYear=" + this.drpYear.SelectedValue;
        object[] _value = new object[1];
        DataTable dt = new DataTable();
        string plansendstate = "";
        for (int i = 0; i < plan.Length; i++)
        {
            plansendstate = plan[i];
            plansendstate = plansendstate.Substring(plansendstate.Length - 2, 1);
            plan[i] = plan[i].Remove(plan[i].Length - 2, 1);
            if (Convert.ToInt32(plansendstate) == 4)
            {
                plan[i] = link + plan[i];
                plan[i] = plan[i].Insert(plan[i].IndexOf("["), url + "'>");
                plan[i] = plan[i].Insert(plan[i].IndexOf("["), "<span style='background:#19BCBE;'><span style='color:red;'>");
                plan[i] = plan[i].Insert(plan[i].IndexOf("]") + 1, "</span>");
                plan[i] += "</span></a>";
            }
            if (Convert.ToInt32(plansendstate) == 5)
            {
                plan[i] = link + plan[i];
                plan[i] = plan[i].Insert(plan[i].IndexOf("["), url + "'>");
                plan[i] = plan[i].Insert(plan[i].IndexOf("["), "<span style='background:#32B44A;'><span style='color:red;'>");
                plan[i] = plan[i].Insert(plan[i].IndexOf("]") + 1, "</span>");
                plan[i] += "</span></a>";
            }
            if (Convert.ToInt32(plansendstate) == 6)
            {
                plan[i] = link + plan[i];
                plan[i] = plan[i].Insert(plan[i].IndexOf("["), url + "'>");
                plan[i] = plan[i].Insert(plan[i].IndexOf("["), "<span style='background:#F3EC19;'><span style='color:red;'>");
                plan[i] = plan[i].Insert(plan[i].IndexOf("]") + 1, "</span>");
                plan[i] += "</span></a>";
            }
            if (Convert.ToInt32(plansendstate) == 7)
            {
                plan[i] = link + plan[i];
                plan[i] = plan[i].Insert(plan[i].IndexOf("["), url + "'>");
                plan[i] = plan[i].Insert(plan[i].IndexOf("["), "<span style='background:#ED1F24;'><span style='color:red;'>");
                plan[i] = plan[i].Insert(plan[i].IndexOf("]") + 1, "</span>");
                plan[i] += "</span></a>";
            }
            if (Convert.ToInt32(plansendstate) < 4)
            {
                plan[i] = link + plan[i];
                plan[i] = plan[i].Insert(plan[i].IndexOf("["), url + "'>");
                plan[i] = plan[i].Insert(plan[i].IndexOf("["), "<span style='color:red;'>");
                plan[i] = plan[i].Insert(plan[i].IndexOf("]") + 1, "</span>");
                plan[i] += "</a>";
            }
            sb.Append(plan[i]);
        }
        return sb.ToString();
    }



    /// <summary>
    /// 插入节假日行到目标GridView
    /// </summary>
    /// <param name="startDay">起始时间</param>
    /// <param name="endDay">结束时间</param>
    /// <param name="holidayName">假期名称</param>
    /// <param name="rowIndex">行索引</param>
    void MergeRow(GridView _gdv)
    {
        DataTable holiday = new DataTable();
        HolidayMgr dllHolidayMgr = new HolidayMgr();
        object[] _value = new object[1];
        for (int i = 0; i < _gdv.Rows.Count; i++)
        {
            _value[0] = Convert.ToDateTime(_gdv.Rows[i].Cells[1].Text);
            holiday = dllHolidayMgr.get(HolidayMgr.field.HolidayStartDay.ToString(), _value);
            if (holiday.Rows.Count > 0)
            {
                GridViewRow gvr = _gdv.Rows[i];
                gvr.Cells[0].Text = Convert.ToDateTime(holiday.Rows[0]["HolidayStartDay"].ToString()).ToShortDateString() + "-" + Convert.ToDateTime(holiday.Rows[0]["HolidayEndDay"].ToString()).ToShortDateString() + " 日   " + holiday.Rows[0]["HolidayName"].ToString();
                for (int j = 1; j < this.gdvTotalPlan.Rows[i].Cells.Count; j++)
                {
                    _gdv.Rows[i].Cells[j].Visible = false;

                }
                gvr.Cells[0].ColumnSpan = this.gdvTotalPlan.Rows[i].Cells.Count;
                gvr.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            }
        }
    }




    /// <summary>
    /// 得到节假日
    /// </summary>
    DataTable getHoliday()
    {
        object[] _value = new object[1];
        _value[0] = this.drpYear.SelectedItem.Text;
        HolidayMgr dllHolidayMgr = new HolidayMgr();
        return dllHolidayMgr.get(HolidayMgr.field.HolidayYear.ToString(), _value);
    }



    /// <summary>
    /// 年份改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGridView();
    }





    /// <summary>
    /// GridView绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvTotalPlan_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            e.Row.Cells[1].Text = Convert.ToDateTime(e.Row.Cells[1].Text).ToShortDateString();

        }
    }



    /// <summary>
    /// 行创建事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvTotalPlan_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridViewRow rowHeader = e.Row;
            TableCell headerCell = e.Row.Cells[1];
            headerCell.Text = "完成时间";
            headerCell = e.Row.Cells[0];
        }
        if (e.Row.RowType == DataControlRowType.DataRow ||
             e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
        }

    }

    /// <summary>
    /// 添加总表计划事件 admin专用
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string _url = "PopTotalPlanAdd.aspx?TotalYear=" + this.drpYear.SelectedItem.Text+"&Url=TotalPlanDisplay.aspx";
        Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=250,width=900,top=200,left=100,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
    }



    /// <summary>
    /// 排序事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSort_Click(object sender, EventArgs e)
    {
        string plan = "";
        int TotalPlanSortId = 0;
        object[] _value = new object[2];
        DataTable dt = new DataTable();
        TotalPlanMgr dllTotalPlanMgr = new TotalPlanMgr();
        for (int i = 0; i < this.gdvTotalPlan.Rows.Count; i++)
        {
            for (int j = 2; j < this.gdvTotalPlan.Rows[i].Cells.Count; j++)
            {
                if (this.gdvTotalPlan.Rows[i].Cells[j].Text != "&nbsp;")
                {

                    plan = this.gdvTotalPlan.Rows[i].Cells[j].Text;
                    plan = plan.Trim();
                    if (plan.Length > 0)
                    {
                        plan = plan.Remove(plan.LastIndexOf("；"));

                        string[] plancontent = plan.Split('；');
                        for (int k = 0; k < plancontent.Length; k++)
                        {
                            TotalPlanSortId++;
                            string QuarterPlanId = plancontent[k];
                            QuarterPlanId = QuarterPlanId.Substring(QuarterPlanId.IndexOf("QuarterPlanId="));
                            QuarterPlanId = QuarterPlanId.Remove(QuarterPlanId.IndexOf("'>"));
                            QuarterPlanId = QuarterPlanId.Remove(QuarterPlanId.IndexOf("&"));
                            QuarterPlanId = QuarterPlanId.Substring(QuarterPlanId.IndexOf("=") + 1);
                            _value[0] = Convert.ToInt32(QuarterPlanId);
                            _value[1] = TotalPlanSortId.ToString();
                            int result = dllTotalPlanMgr.editTotalPlanSortId(_value);
                            if (result == 0)
                            {
                                Utility.Error.RedirectErrorPage("排序未成功，请重试！");
                            }
                        }
                    }
                }
            }
        }
        bindGridView();
    }


    protected void btnAdd_Command(object sender, CommandEventArgs e)
    {
        string _url = "PopTotalPlanAdd.aspx?TotalYear=" + this.drpYear.SelectedItem.Text + "&Url=" + e.CommandArgument.ToString();
        Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=250,width=900,top=200,left=100,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
    }
}
